﻿#include "SL-List-Base.h"
#include "MemMana.h"
#include "stdio.h"
#include "string.h"
#include "operations.h"

#define Malloc(x) ZMalloc(0, x)
#define Free(x) ZFree(x)

static void __SLL_DF_Visit(pSLListNode_t node, uint32_t level)
{
    printf("node[%d]: %#lX\r\n", level, (uint64_t) node);
}

pSLList_t __SLL_Create()
{
    pSLList_t list = (pSLList_t) Malloc(sizeof(SLList_t));
    list->free = NULL;
    list->head = NULL;
    list->tail = NULL;
    list->compare = NULL;
    list->visit = __SLL_DF_Visit;
    return list;
}

void __SLL_CreateStatic(pSLList_t list)
{
    if (list == NULL)
        return;

    list->free = NULL;
    list->head = NULL;
    list->tail = NULL;
    list->compare = NULL;
    list->visit = __SLL_DF_Visit;
}

void SLL_Delete(pSLList_t list)
{
    if (list == NULL || list->free == NULL)
        return;

    pSLListNode_t now = list->head;

    while (now != NULL)
    {
        pSLListNode_t next = now->next;
        list->free(now);
        now = next;
    }
    memset(list, 0, sizeof(SLList_t));
    Free(list);
}

pSLListNode_t SLL_Head(pSLList_t list)
{
    return list == NULL ? NULL : list->head;
}

pSLListNode_t SLL_Tail(pSLList_t list)
{
    return list == NULL ? NULL : list->tail;
}

uint8_t __SLL_Insert(pSLList_t list, pSLListNode_t node)
{
    if (list == NULL || node == NULL)
        return 0;

    node->next = NULL;

    if (list->head == NULL)
        list->head = node;

    if (list->tail)
        list->tail->next = node;

    list->tail = node;
    return 1;
}

uint8_t __SLL_DeleteNode(pSLList_t list, pSLListNode_t node)
{
    if (list == NULL || list->head == NULL || node == NULL)
        return 0;

    pSLListNode_t now = list->head;
    if (node != now)//删除的不是头结点的情况
    {
        while (now)
        {
            if (now->next == node)
                break;
            now = now->next;
        }

        //找到now为node的上一个节点,或没找到,now为NULL
        if (now)
        {
            now->next = node->next;//将node踢出链表

            if (node == list->tail)
                list->tail = now;//删掉的是最后一个节点,那么当前的最后就是之前最后的前一个

            if (node == list->head)
                list->head = now->next;//删掉的是第一个节点,那么当前的第一个就是之前的后一个

            list->free(node);//释放node的内存
            return 1;
        }
    }
    else//删除第一个结点
    {
        list->head = now->next;//把头结点踢出链表
        list->free(now);//释放now的内存
        return 1;
    }

    return 0;
}

pSLListNode_t __SLL_GetNode(pSLList_t list, uint32_t index)
{
    if (list == NULL)
        return NULL;

    pSLListNode_t node = list->head;

    while (node != NULL && index != 0)
    {
        node = node->next;
        index--;
    }

    return index == 0 ? node : NULL;
}

pSLListNode_t SLL_Search(pSLList_t list, void* target)
{
    if (list == NULL || target == NULL)
        return NULL;

    pSLListNode_t now = list->head;

    while (now != NULL)
    {
        if (list->compare(now, target) == SLL_Equal)//找到对应数据
            return now;
        now = now->next;
    }

    return NULL;
}

static uint32_t num;
static void __SLL_Count(pSLListNode_t node, uint32_t level)
{
    num = level;
}

uint32_t SLL_Length(pSLList_t list)
{
    if (list == NULL)
        return 0;

    num = 0;

    __SLL_Visit visit_fn = list->visit;
    list->visit = __SLL_Count;

    __SLL_Traverse(list);

    list->visit = visit_fn;

    return num + 1;
}

void __SLL_Traverse(pSLList_t list)
{
    if (list == NULL || list->visit == NULL)
        return;

    uint32_t level = 0;
    pSLListNode_t now = list->head;

    while (now != NULL)
    {
        pSLListNode_t next = now->next;
        list->visit(now, level++);
        now = next;
    }
}


